

<script type="text/javascript" language="Javascript">
  $(document).ready(function() {
//       $('#ui-widget-header-text').html('Question')
//      textboxes = $("input:visible:not([disabled],[readonly]),select,button");
//    if ($.browser.mozilla) {
//        $(textboxes).keypress (checkForEnter);
//    } else {
//        $(textboxes).keydown (checkForEnter);
//    }
//
//    $("#gFormQuestion").validationEngine({   //    client side validation
//      isOverflown: true,
//      overflownDIV: ".ui-layout-center"
//    });
//    $("#gFormQuestion").validationEngine('attach');
//    reset_form("#gFormQuestion");

      reset_form();
      $("#flex-grid-table").flexigrid({

          url: "${resource(dir:'question', file:'list')}",
          dataType: 'json',
          colModel : [
              {display: 'SL', name : 'sl', width : 150, sortable : true, align: 'center',process:executeEditQuestion},
              {display: 'ID', name : 'id', width : 180, sortable : true, align: 'left',hide: true,process:executeEditQuestion},
              {display: 'Answering Type',name : 'Answering Type', width : 150, sortable : true, align: 'center' ,process:executeEditQuestion},
              {display: 'Description',name : 'description', width : 180, sortable : true, align: 'left',process:executeEditQuestion},
              {display: 'Note',name : 'note', width : 180, sortable : true, align: 'left',process:executeEditQuestion},
              {display: 'Project',  name : 'project', width : 180, sortable : true, align: 'left',process:executeEditQuestion},
              {display: 'Question', name : 'question', width : 180, sortable : true, align: 'left',process:executeEditQuestion}
          ],
          buttons : [
          ],

          searchitems : [
              {display: 'Project', name : 'project'}
//              {display: 'Name', name : 'name', isdefault: true}
          ],
          sortname: "name",
          sortorder: "asc",
          usepager: true,
          title: 'All Questions Information',
          useRp: true,
          rp: 15,
          showTableToggleBtn: true,
          width: 800,
          height: 200,
          singleSelect:true
      });
  });
  function getSelectedQuestionId()
  {
    var questionInstanceId = null;
    var rowid = $("#jqgrid-grid").jqGrid('getGridParam', 'selrow');
    if(rowid)
    {
      questionInstanceId = $("#jqgrid-grid").jqGrid('getCell', rowid, 'id');
    }
    if(questionInstanceId == null){
      questionInstanceId = $('#gFormQuestion input[name = id]').val();
    }
    return questionInstanceId;
  }
  function executePreConditionQuestion() {
      // trim field vales before process.
//      trim_form();
//      if ($("#gFormQuestion").validate().form({onfocusout: false}) == false) {
//        return false;
//      }
      return true;
  }
  function executeAjaxQuestion() {
    if(!executePreConditionQuestion()) {
      return false;
    }
    var actionUrl = null;
    if ($('#gFormQuestion input[name = id]').val()) {
      actionUrl = "${request.contextPath}/${params.controller}/update";
    } else {
      actionUrl = "${request.contextPath}/${params.controller}/create";
    }
    jQuery.ajax({
      type:'post',
      data:jQuery("#gFormQuestion").serialize(),
      url: actionUrl,
      success:function(data, textStatus) {
          executePostConditionQuestion(data);
      },
      error:function(XMLHttpRequest, textStatus, errorThrown) {
      },
      complete:function(){
      },
      dataType:'json'
    });
    return false;
  }
  function executePostConditionQuestion(result) {
      if (result.type == 1) {
          $("#flex-grid-table").flexReload();
        reset_form('#gFormQuestion');
      }
      MessageRenderer.render(result);
  }
  function deleteAjaxQuestion() {    // Delete record
    var questionInstanceId = getSelectedQuestionId();
    if(executePreConditionForDeleteQuestion(questionInstanceId))
    {
        $.ajax({
            type: "POST",
            dataType: "json",
            data:jQuery("#gFormQuestion").serialize(),
            url: "${resource(dir:'question', file:'delete')}",
            success: function(message) {
                executePostConditionForDeleteQuestion(message);
            }
        });

      %{--$("#dialog").dialog("destroy");--}%
      %{--$("#dialog-confirm-questionInstance").dialog({--}%
        %{--resizable: false,--}%
        %{--height:150,--}%
        %{--modal: true,--}%
        %{--title: 'Delete alert',--}%
        %{--buttons: {--}%
          %{--'Delete item(s)': function() {--}%
            %{--$(this).dialog('close');--}%
            %{--$.ajax({--}%
              %{--type: "POST",--}%
              %{--dataType: "json",--}%
              %{--data:jQuery("#gFormQuestion").serialize(),--}%
              %{--url: "${resource(dir:'questionInstance', file:'delete')}",--}%
              %{--success: function(message) {--}%
                %{--executePostConditionForDeleteQuestion(message);--}%
              %{--}--}%
            %{--});--}%
          %{--},--}%
          %{--Cancel: function() {--}%
            %{--$(this).dialog('close');--}%
          %{--}--}%
        %{--}--}%
      %{--}); //end of dialog--}%
    }
  }

  function executePreConditionForEditQuestion(questionInstanceId) {
    if(questionInstanceId == null)
    {
      alert("Please select a questionInstance to edit") ;
      return false;
    }
    return true;
  }
  function executeEditQuestion(celDiv,projectInstanceId) {
    %{--var questionInstanceId = getSelectedQuestionId();--}%
    %{--if(executePreConditionForEditQuestion(questionInstanceId))--}%
    %{--{--}%
      %{--$.ajax({--}%
        %{--type: "POST",--}%
        %{--url: "${resource(dir:'questionInstance', file:'edit')}?id="+ questionInstanceId,--}%
        %{--success: function(entity) {--}%
          %{--executePostConditionForEditQuestion(entity);--}%
        %{--},--}%
        %{--dataType:'json'--}%
      %{--});--}%
    %{--}--}%

      $(celDiv).click(function () {
          if (projectInstanceId) {
              $.ajax({
                  type:"POST",
                  url:"${resource(dir:'question', file:'edit')}?id=" + projectInstanceId,
                  success:function (entity) {
                      executePostConditionForEditQuestion(entity);
                  },
                  dataType:'json'
              });
          }
      });
  }
  function executePostConditionForEditQuestion(data) {
      if (data == null) {
        alert('Selected questionInstance might have been deleted by someone');  //Message renderer
      } else {
        showQuestion(data);
      }
  }
  function executePreConditionForDeleteQuestion(questionInstanceId) {
    if(questionInstanceId == null)
    {
      alert("Please select a questionInstance to delete") ;
      return false;
    }
    return true;
  }
  function executePostConditionForDeleteQuestion(data) {
      if (data.type ==1) {
          $("#flex-grid-table").flexReload();
         reset_form('#gFormQuestion');
      }
      MessageRenderer.render(data)
  }
  function showQuestion(entity) {
    $('#gFormQuestion input[name = id]').val(entity.id);
    $('#gFormQuestion input[name = version]').val(entity.version);
    
    if(entity.answeringType){
       $('#answeringType').val(entity.answeringType.id).attr("selected","selected");
    }
    else{
        $('#answeringType').val(entity.answeringType);
    }
    $('#description').val(entity.description);
    $('#note').val(entity.note);
    if(entity.project){
       $('#project').val(entity.project.id).attr("selected","selected");
    }
    else{
        $('#project').val(entity.project);
    }
    $('#question').val(entity.question);
    $('#create-button').attr('value', 'Update');
     $('#cancel-button').attr('value', 'Cancel');
    $('#delete-button').show();
  }
  function executeSearchQuestion(fieldName, fieldValue){
    if(executePreConditionForSearchQuestion(fieldName, fieldValue))
    {
      $.ajax({
        type: "POST",
        url: "${resource(dir:'questionInstance', file:'search')}?fieldName="+fieldName+"&fieldValue="+fieldValue,
        success: function(entity) {
          executePostConditionForSearchQuestion(entity, fieldName, fieldValue);
        },
        dataType:'json'
      });
    }
  }
  function executePreConditionForSearchQuestion(fieldName,fieldValue) {
      // trim field vales before process.
      $('#'+fieldName).val($.trim($('#'+fieldName).val()));
      if(fieldValue == '' || fieldValue == null){
          reset_form("#gFormQuestion");
          return false;
      }
      return true;
  }
  function executePostConditionForSearchQuestion(data, fieldName, fieldValue) {
      if (data == null) {
          reset_form("#gFormQuestion"); // Clear Form
          $('#'+fieldName).val(fieldValue); // Set search field with fieldValue
      } else {
        showQuestion(data);
      }
  }
</script>